package com.ssy.lingxi.order.serviceimpl.base;

import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.common.utils.CollectionPageUtil;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.request.MemberFeignVO;
import com.ssy.lingxi.order.entity.PlatformPurchaseProcessDO;
import com.ssy.lingxi.order.entity.PlatformPurchaseProcessMemberDO;
import com.ssy.lingxi.order.model.dto.OrderMemberQueryDTO;
import com.ssy.lingxi.order.model.vo.basic.request.OrderMemberIdAndRoleIdVO;
import com.ssy.lingxi.order.repository.PlatformPurchaseProcessMemberRepository;
import com.ssy.lingxi.order.service.base.IBasePlatformPurchaseProcessMemberService;
import com.ssy.lingxi.order.service.feign.IMemberFeignService;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 平台后台 - 采购流程关联的会员相关接口实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-07-27
 */
@Service
public class BasePlatformPurchaseProcessMemberServiceImpl implements IBasePlatformPurchaseProcessMemberService {
    @Resource
    private PlatformPurchaseProcessMemberRepository platformPurchaseProcessMemberRepository;

    @Resource
    private IMemberFeignService memberFeignService;

    /**
     * 校验、新增采购流程关联的会员
     *
     * @param purchaseProcess 采购流程
     * @param allMembers      是否适用于所有会员
     * @param memberList      会员列表
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> checkMembers(PlatformPurchaseProcessDO purchaseProcess, Boolean allMembers, List<OrderMemberIdAndRoleIdVO> memberList) {
        if(allMembers) {
            purchaseProcess.setMembers(new HashSet<>());
            return Wrapper.success();
        }

        List<PlatformPurchaseProcessMemberDO> members = memberList.stream().map(memberVO -> {
            PlatformPurchaseProcessMemberDO processMember = new PlatformPurchaseProcessMemberDO();
            processMember.setProcess(purchaseProcess);
            processMember.setMemberId(memberVO.getMemberId());
            processMember.setRoleId(memberVO.getRoleId());
            return processMember;
        }).collect(Collectors.toList());

        platformPurchaseProcessMemberRepository.saveAll(members);

        purchaseProcess.setMembers(new HashSet<>(members));
        return Wrapper.success();
    }

    /**
     * 分页查询采购流程关联的会员列表
     *
     * @param purchaseProcess 采购流程
     * @param name            会员名称
     * @param current         当前页
     * @param pageSize        每页行数
     * @return 查询结果
     */
    @Override
    public Wrapper<PageData<OrderMemberQueryDTO>> pageMembers(PlatformPurchaseProcessDO purchaseProcess, String name, int current, int pageSize) {
        if(purchaseProcess.getAllMembers()) {
            return Wrapper.success(new PageData<>(0L, new ArrayList<>()));
        }

        List<PlatformPurchaseProcessMemberDO> members = platformPurchaseProcessMemberRepository.findByProcess(purchaseProcess, Sort.by("id").ascending());
        if(CollectionUtils.isEmpty(members)) {
            return Wrapper.success(new PageData<>(0L, new ArrayList<>()));
        }

        //从会员服务查询
        List<MemberFeignVO> feignList = members.stream().map(member -> {
            MemberFeignVO feignVO = new MemberFeignVO();
            feignVO.setMemberId(member.getMemberId());
            feignVO.setRoleId(member.getRoleId());
            return feignVO;
        }).collect(Collectors.toList());

        Wrapper<List<OrderMemberQueryDTO>> feignResult = memberFeignService.findPlatformMembers(name, feignList);
        if(feignResult.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(feignResult.getCode(), feignResult.getMessage());
        }

        if(CollectionUtils.isEmpty(feignResult.getData())) {
            return Wrapper.success(new PageData<>(0L, new ArrayList<>()));
        }

        return Wrapper.success(new PageData<>((long) feignResult.getData().size(), CollectionPageUtil.pageList(feignResult.getData(), current, pageSize)));
    }

    /**
     * 校验、修改采购流程关联的会员
     *
     * @param purchaseProcess 采购流程
     * @param allMembers      是否适用于所有会员
     * @param memberList      会员列表
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> updateMembers(PlatformPurchaseProcessDO purchaseProcess, Boolean allMembers, List<OrderMemberIdAndRoleIdVO> memberList) {
        if(allMembers) {
            platformPurchaseProcessMemberRepository.deleteByProcess(purchaseProcess);
            purchaseProcess.setMembers(new HashSet<>());
            return Wrapper.success();
        }

        //先删除再保存
        platformPurchaseProcessMemberRepository.deleteByProcess(purchaseProcess);

        List<PlatformPurchaseProcessMemberDO> members = memberList.stream().map(memberVO -> {
            PlatformPurchaseProcessMemberDO processMember = new PlatformPurchaseProcessMemberDO();
            processMember.setProcess(purchaseProcess);
            processMember.setMemberId(memberVO.getMemberId());
            processMember.setRoleId(memberVO.getRoleId());
            return processMember;
        }).collect(Collectors.toList());

        platformPurchaseProcessMemberRepository.saveAll(members);

        purchaseProcess.setMembers(new HashSet<>(members));
        return Wrapper.success();
    }
}
